//
//  MineCell.m
//  GC_OC
//
//  Created by xyy on 2022/5/23.
//

#import "MineCell.h"

@interface MineCell ()

@end

@implementation MineCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleGray;
        self.contentView.backgroundColor = [UIColor whiteColor];
    
        
//        self.container = [UIView new];
//        self.container.backgroundColor = [UIColor whiteColor];
//        [self.contentView addSubview:self.container];
        
        self.leftIcon = [UIImageView new];
        [self.contentView addSubview:self.leftIcon];
        
        self.name = [UILabel new];
        self.name.textColor = [UIColor blackColor];
        self.name.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        [self.contentView addSubview:self.name];
        
        self.detail = [UILabel new];
        self.detail.textAlignment = NSTextAlignmentRight;
        self.detail.textColor = [UIColor blackColor];
        self.detail.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        [self.contentView addSubview:self.detail];
        
        self.rightIcon = [UIButton new];
        [self.rightIcon addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.rightIcon];
        
        UISwitch *switchBtn = [[UISwitch alloc] init];
        [self.contentView addSubview:switchBtn];
        self.switchBtn = switchBtn;
        switchBtn.hidden = true; // 设置默认隐藏
        switchBtn.on = true;
        switchBtn.onTintColor = fkMainColor;
        [switchBtn addTarget:self action:@selector(switchBtnChanged:) forControlEvents:UIControlEventValueChanged];
        
        UIView *lineView = [UIView new];
        lineView.backgroundColor = XHUIColorFromRGBA(0x000000, 0.1);
        [self.contentView addSubview:lineView];
        
//        [self.container mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.equalTo(self.contentView);
//        }];
        
        [self.leftIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(30);
            make.width.height.mas_equalTo(24);
            make.centerY.offset(0);
        }];
        [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.leftIcon.mas_right).offset(14);
            make.centerY.height.equalTo(self.leftIcon);
            make.width.mas_equalTo(120);
        }];
        [self.detail mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.rightIcon.mas_left).offset(-10);
            make.centerY.height.equalTo(self.leftIcon);
            make.width.mas_equalTo(150);
        }];
        [switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-32);
            make.centerY.offset(0);
        }];
        [self.rightIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-32);
            make.width.height.mas_equalTo(32);
            make.centerY.equalTo(self.name);
        }];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.name).offset(-5);
            make.right.equalTo(self.rightIcon).offset(0);
            make.height.mas_equalTo(1);
            make.bottom.offset(0);
        }];
    }
    return self;
}

- (void)rightAction {
    if (self.rightBlock != nil ) {
        self.rightBlock(self.info);
    }
}

- (void)switchBtnChanged:(UISwitch *)switchBtn
{
    NSString *name = self.info[@"name"];
    if ([name isEqualToString:@"历史记录"]) {
        [FKCacheTool setHistoryRecord:switchBtn.isOn];
    } else if ([name isEqualToString:@"搜索记录"]) {
        [FKCacheTool setSearchRecord:switchBtn.isOn];
    }
}

@end
